package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/admin/role/")
public class AdminRoleController extends HttpServlet {
    @Autowired
    MarketRoleMapper mapper;
    @GetMapping("options")
    private void options(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        List<MarketRole> marketRoles = mapper.selectByExample(marketRoleExample);
        List<MarketRoleVo> marketRoleVoList = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            marketRoleVoList.add(new MarketRoleVo(marketRole.getName(),marketRole.getId()));
        }
        Object o = ResponseUtil.okList(marketRoleVoList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }
}
